rm(list=ls())


## This script takes the environment saved in Preparing_covariate_balance.R to make 
# scatter plots of covariate balance, allowing for treatment reversal

load("data/tmp/PanelMatch_temp1.RData")


max.lead <- 0
# some final preparation  
all_lines_entry <- 
    list(all_entry$L3_West[stringr::str_detect(names(all_entry$L3_West), "no ") & 
                                      substr(names(all_entry$L3_West), 3, 3) == max.lead & 
                                      stringr::str_detect(names(all_entry$L3_West), "none")][[1]],
         all_entry$L3_West[stringr::str_detect(names(all_entry$L3_West), "no ") & 
                                      substr(names(all_entry$L3_West), 3, 3) == max.lead & 
                                      stringr::str_detect(names(all_entry$L3_West), "none") == FALSE][[1]],
         all_entry$L3_West[stringr::str_detect(names(all_entry$L3_West), "no ") == FALSE &
                                      stringr::str_detect(names(all_entry$L3_West), "mahalanobis 5") & 
                                      substr(names(all_entry$L3_West), 3, 3) == max.lead][[1]],
         all_entry$L3_West[stringr::str_detect(names(all_entry$L3_West), "no ") == FALSE &
                                      stringr::str_detect(names(all_entry$L3_West), "CBPS.match 5") & 
                                      substr(names(all_entry$L3_West), 3, 3) == max.lead][[1]],      
         all_entry$L3_West[stringr::str_detect(names(all_entry$L3_West), "no ") == FALSE &
                                      stringr::str_detect(names(all_entry$L3_West), "CBPS.weight 10") & 
                                      substr(names(all_entry$L3_West), 3, 3) == max.lead][[1]],

         all_entry$L3_East[stringr::str_detect(names(all_entry$L3_East), "no ") & 
                                     substr(names(all_entry$L3_East), 3, 3) == max.lead & 
                                     stringr::str_detect(names(all_entry$L3_East), "none")][[1]],
         all_entry$L3_East[stringr::str_detect(names(all_entry$L3_East), "no ") & 
                                     substr(names(all_entry$L3_East), 3, 3) == max.lead & 
                                     stringr::str_detect(names(all_entry$L3_East), "none") == FALSE][[1]],
         all_entry$L3_East[stringr::str_detect(names(all_entry$L3_East), "no ") == FALSE &
                                     stringr::str_detect(names(all_entry$L3_East), "mahalanobis 5") & 
                                     substr(names(all_entry$L3_East), 3, 3) == max.lead][[1]],
         all_entry$L3_East[stringr::str_detect(names(all_entry$L3_East), "no ") == FALSE &
                                     stringr::str_detect(names(all_entry$L3_East), "CBPS.match 5") & 
                                     substr(names(all_entry$L3_East), 3, 3) == max.lead][[1]],      
         all_entry$L3_East[stringr::str_detect(names(all_entry$L3_East), "no ") == FALSE &
                                     stringr::str_detect(names(all_entry$L3_East), "CBPS.weight 10") & 
                                     substr(names(all_entry$L3_East), 3, 3) == max.lead][[1]]
         
    )



### setting it up ###
plot_matrix <- matrix(1:length(all_lines_entry), nrow = 2)
plot_matrix[1, ] <- 1:(length(all_lines_entry)/2)
plot_matrix[2, ] <- (length(all_lines_entry)/2+1):(length(plot_matrix[1, ]) + length(plot_matrix[2, ]))



file = file.path("output/figures/", "figure_2.pdf")
pdf(file = file, width = 10, height = 6)
layout(plot_matrix, heights = c(1.5, 1.5))

par(oma = c(6.5, 5, 6.5, 0))
par(mar = c(1.5, 3.5, 2 , 1))

for (i in 1:length(all_lines_entry)) {
    plot(1, 1,
         type = "l",
         xlim = c(0.5, 3.5),
         ylim = c(-1.1, 1.1),
         xlab = "", ylab = "",
         xaxt = "n")
    abline(v = 3, lty = "dashed")
    abline(h = 0, lty = "dotted")
    axis(side = 1, 
         at = 1:3, 
         labels = c(-3,
                    -2, -1))
    for (k in 1:ncol(all_lines_entry[[i]])) {
        if (i == 1) {
            # mtext("Democratization", side = 2, line = 4.0, outer=FALSE,
            #       cex = 0.8)
            mtext("Standardized Mean \n Differences for \n Greens Entry", 
                  side = 2, line = 2.2, outer=FALSE,
                  cex = 0.8)
        } else if (i == 2) {
            mtext(3, line = 0.3, text = "")
        } else if (i == 3) {
            mtext(3, line = 0.3, text = "",cex = 0.8)
        } else if (i == 4) {
            mtext(3, line = 0.3, text = "",cex = 0.8)
        } else if (i == 5) {
            mtext(3, line = 0.3, text = "")
        } else if (i == 6) {
            # mtext("Authoritarian Reversal", side = 2, line = 4.0, outer=FALSE,
            #       cex = 0.8)
            mtext("Standardized Mean \n Differences for \n Greens Entry", 
                  side = 2, line = 2.2, outer=FALSE,
                  cex = 0.8)
        }
        if (k != 1){
            lines(1:3, all_lines_entry[[i]][,k][1:3],
                  col = alpha("black", 0.2), lwd = 1)
            
        } else {
            lines(1:3, all_lines_entry[[i]][,k][1:3],
                  col = "black", lwd =1.5)
        }
    }
}

mtext("Mahalanobis Distance \n Matching", line = -1.5, at = 0.52, outer = TRUE)
mtext("Propensity Score \n Matching", line = -1.5, at = 0.72, outer = TRUE)
mtext("Propensity Score \n Weighting", line = -1.5, at = 0.915, outer = TRUE)
mtext("Before \n Matching", line = -1.5, at = 0.115, outer = TRUE)
mtext("Before \n Refinement", line = -1.5, at = 0.317, outer = TRUE)
mtext(2, text = "West Germany", line = 3,
      at = 0.74, outer = TRUE, cex = 1)
mtext(2, text = "East Germany", line = 3,
      at = 0.24, outer = TRUE, cex = 1)
mtext(1, text = "Elections relative to the administration of treatment", line = 1.2,
      at = 0.52, outer = TRUE, cex = 1)
dev.off()


